package com.backup;

import java.io.File;
import java.io.IOException;
import java.util.Date;

import com.backup.bean.FileObject;
import com.backup.util.PropertiesUtil;

public class BackupTask extends Thread{
	private PropertiesUtil pUtil = new PropertiesUtil("config.properties");
	//运行模式
	//every	每一月、日、时、分，time的格式为00:00:00 23:58:00
	//interval	间隔（毫秒）time的毫秒数
//	private String runMode = pUtil.getProperties("runMode", "interval");
	private String time = pUtil.getProperties("time", "60000");//间隔时间
	private String srcPath = pUtil.getProperties("srcPath");//源目录
	private String targetPath = pUtil.getProperties("targetPath");//备份目录
	public void run(){
		while(true){
			System.out.println(">>>>>>>>>>>>");
			//读取配置信息
			long startTime = new Date().getTime();
			//开始进行备份
			try {
				startBackup();
			} catch (Exception e) {
				e.printStackTrace();
			}
			long endtime = new Date().getTime();
			//如果是间隔
//			if("interval".equalsIgnoreCase(runMode)){
				long interval = 60000;
				try {
					interval = Long.parseLong(time);
				} catch (Exception e1) {
					e1.printStackTrace();
				}
				if(interval<1*60*1000){
					interval=1*60*1000;//至少1分钟同步一次
				}
				if(endtime-startTime<interval){
					try {
						sleep(interval-(endtime-startTime));
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
				}
		}
	}
	
	private void startBackup()throws Exception{
		if(!new java.io.File(srcPath).exists()){
			throw new IOException("源目录["+srcPath+"]不存在");
		}
		CompareFolder cf = new CompareFolder();
		FileObject fo = cf.readFileObject(new File(srcPath),  new File(targetPath));
		cf.startCompare(fo);
	}
}
